package bday.tvlport.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import bday.tvlport.mail.SendMailTLS;
import bday.tvlport.utils.LoggerUtility;

@ManagedBean(name="contact")
@RequestScoped
public class ContactMailer {

	private String name;
	private String message;
	
	@ManagedProperty("#{returnMsg}")
	private String returnMsg;
	
	public ContactMailer() {
		
	}
	public ContactMailer(String name, String message) {
		this.name = name;
		this.message = message;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	public String getReturnMsg() {
		return returnMsg;
	}
	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}
	public String sendMessage() {
		returnMsg = "error";
		try{
			String text = "Name: " + name + "\nMessage: " + message;
			SendMailTLS.mail(null, text , true);
			HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
			session.setAttribute("contact", "success");
			returnMsg = "success";
		} catch (Throwable e) {
			LoggerUtility.error(e.toString());
		}
		reset();
		return returnMsg;
	}
	
	private void reset() {
		setName(null);
		setMessage(null);
	}
}
